import Vue from 'vue'
import sm4 from "./sm4.js"
import sm2 from "./sm2/index.js"
//import base64js from "./base64.js"
//import wlnAuth  from "./components/auth.vue"
//import wlnEmpty from "./components/empty.vue"
//import wlnDataPicker from "./components/data-picker.vue"
//import cuCustom from './colorui/components/cu-custom.vue'
/* 引入框架css样式 */
import "./utility.js"
import "./style.css"
let components = {
	//wlnAuth,
	//wlnEmpty,
	//wlnDataPicker,
	//cuCustom
}
export default {
	install(Vue) {
		
		Vue.prototype.platform = 'h5';
		Vue.prototype.TitleBar = true;
		
		// #ifdef APP-PLUS
		Vue.prototype.platform = 'app' //App平台
		// #endif
		
		// #ifdef MP
		Vue.prototype.platform = 'mp' //通用小程序平台
		Vue.prototype.TitleBar = false;
		// #endif
		
		// #ifdef MP-WEIXIN
		Vue.prototype.platform = 'mp-weixin' //微信小程序
		Vue.prototype.TitleBar = false;
		// #endif
		
		// #ifdef MP-ALIPAY
		Vue.prototype.platform = 'mp-alipay' //支付宝小程序
		Vue.prototype.TitleBar = true;
		// #endif
		
		Vue.prototype['wln'] = this
		for(let k in components) { Vue.component(components[k]['name'], components[k]) }
	},
	cfgs: {
		pk: '',
		api: '',
		debug: false,
		color: '#111111',
		bgColor: '#ffffff',
	},
	empty() {},
	config(opts) { if (opts && typeof opts == 'object') { for (let k in opts) { this.cfgs[k] = opts[k] } } },
	toast(msg) {uni.showToast({title: msg, icon: 'none' })},
	alert(msg) {uni.showModal({title: '', showCancel: false, confirmText: '确定', content: msg, success: win => {}}) },
	alert(msg, callFn) {uni.showModal({title: '', showCancel: false, confirmText: '确定', content: msg, success: callFn }) },
	confirm(msg, yesFn, noFn) {uni.showModal({ content: msg, success: (cfm)=> { if(cfm.confirm) { yesFn && yesFn() } if(cfm.cancel) { noFn && noFn()} } }) },
	post(path, callfn, data, encrypt, noAuthorization) {
		let token = ''.randomString(16)
		let header = { Authorization: uni.getStorageSync('ticket') || '' }
		if(typeof data === "undefined") {data = { ticket: header.Authorization } }
		else if(typeof data === 'object' && !data.ticket) { data.ticket = header.Authorization }
		if(encrypt && this.cfgs.pk)
		{
			if(this.cfgs.debug) { console.debug(data) }
			header['sm2token'] = sm2.doEncrypt(token, this.cfgs.pk, 1) // 1 - C1C3C2，0 - C1C2C3，默认为1
			if (typeof data === 'string') {
				data = sm4.encrypt(token, data);
			} else {
				data = sm4.encrypt(token, JSON.stringify(data));
			}
		}
		uni.request({
			url: this.cfgs.api + path, data: data, method: 'POST',
			header: header,
			success: res => {
				let authify = res.header ? res.header['Authify-State'] || '' : ''
				if(noAuthorization !== true && authify === 'false') {
					uni.showModal({
						title: '验证提醒',
						cancelText: '看看再说',
						confirmText: '立即验证',
						content: '此功能需要验证身份，请先验证~',
						success: res => {
							if (res.confirm)
							{
								uni.navigateTo({ url: redirect })
							}
							else
							{
								uni.hideLoading()
							}
						}
					});
				} else if(typeof callfn === 'function') {					
					if(encrypt && this.cfgs.pk && res.data && res.data.data && typeof res.data.data === 'string')
					{
						let plain = sm4.decrypt(token, res.data.data);
						if(plain && (plain[0] == '[' || plain[0] == '{'))
						{
							res.data.data = JSON.parse(plain)
						}
						else
						{
							res.data.data = plain
						}
						if(this.cfgs.debug) { console.debug(res.data) }
					}
					callfn(res.data)
				}
			},
			fail: res => {
				uni.hideLoading()
				this.toast('请求数据失败，请稍后再试')
			}
		});
	},
	setToken(data) {
		if(data) {
			uni.setStorageSync('token', data)
		} else {
			uni.removeStorage({ key: 'token', success: res => { } });
		}
	}
}
if(typeof window !== 'undefined' && window.Vue) {
	for(let k in components) { window.Vue.use(components[k]) }
}